#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class PurchasingRecordProvider : CY.GFive.Core.Providers.DALProvider.IPurchasingRecordProvider
	{
		#region Sql
		private static readonly string SqlSelectPurchasingRecord = "SELECT [Id], [PurchasingItem],[State],[Date],[Amount],[Price],[Unit],[PurchaserName],[TotalValue],[PurchaseCate] FROM [PurchasingRecord] WHERE  [Id]=@Id";

		private static readonly string SqlInsertPurchasingRecord = "INSERT INTO [PurchasingRecord]([PurchasingItem],[State],[Date],[Amount],[Price],[Unit],[PurchaserName],[TotalValue],[PurchaseCate]) values(@PurchasingItem,@State,@Date,@Amount,@Price,@Unit,@PurchaserName,@TotalValue,@PurchaseCate);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdatePurchasingRecord = "UPDATE [PurchasingRecord] SET [PurchasingItem] = @PurchasingItem,[State] = @State,[Date] = @Date,[Amount] = @Amount,[Price] = @Price,[Unit] = @Unit,[PurchaserName] = @PurchaserName,[TotalValue] = @TotalValue,[PurchaseCate] = @PurchaseCate WHERE  [Id]=@Id";

		private static readonly string SqlDeletePurchasingRecord = "DELETE [PurchasingRecord] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllPurchasingRecord = "SELECT [Id], [PurchasingItem],[State],[Date],[Amount],[Price],[Unit],[PurchaserName],[TotalValue],[PurchaseCate] FROM [PurchasingRecord]";
		#endregion

		#region IPurchasingRecordProvider Members
		public Core.Business.PurchasingRecord Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectPurchasingRecord);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.PurchasingRecord purchasingRecord = new Core.Business.PurchasingRecord();

				if (!reader.IsDBNull(0)) purchasingRecord.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) purchasingRecord.PurchasingItem = reader.GetString(1);
				if (!reader.IsDBNull(2)) purchasingRecord.State = reader.GetByte(2);
				if (!reader.IsDBNull(3)) purchasingRecord.Date = reader.GetDateTime(3);
				if (!reader.IsDBNull(4)) purchasingRecord.Amount = reader.GetInt32(4);
				if (!reader.IsDBNull(5)) purchasingRecord.Price = reader.GetDecimal(5);
				if (!reader.IsDBNull(6)) purchasingRecord.Unit = reader.GetString(6);
				if (!reader.IsDBNull(7)) purchasingRecord.PurchaserName = reader.GetString(7);
				if (!reader.IsDBNull(8)) purchasingRecord.TotalValue = reader.GetDecimal(8);
				if (!reader.IsDBNull(9)) purchasingRecord.PurchaseCate = reader.GetString(9);

				return purchasingRecord;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.PurchasingRecord purchasingRecord)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@PurchasingItem", SqlDbType.NVarChar, purchasingRecord.PurchasingItem);
			sql.AddParameter("@State", SqlDbType.TinyInt, purchasingRecord.State);
			sql.AddParameter("@Date", SqlDbType.DateTime, purchasingRecord.Date);
			sql.AddParameter("@Amount", SqlDbType.Int, purchasingRecord.Amount);
			sql.AddParameter("@Price", SqlDbType.Decimal, purchasingRecord.Price);
			sql.AddParameter("@Unit", SqlDbType.NVarChar, purchasingRecord.Unit);
			sql.AddParameter("@PurchaserName", SqlDbType.NVarChar, purchasingRecord.PurchaserName);
			sql.AddParameter("@TotalValue", SqlDbType.Decimal, purchasingRecord.TotalValue);
			sql.AddParameter("@PurchaseCate", SqlDbType.NVarChar, purchasingRecord.PurchaseCate);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertPurchasingRecord);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) purchasingRecord.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.PurchasingRecord purchasingRecord)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@PurchasingItem", SqlDbType.NVarChar, purchasingRecord.PurchasingItem);
			sql.AddParameter("@State", SqlDbType.TinyInt, purchasingRecord.State);
			sql.AddParameter("@Date", SqlDbType.DateTime, purchasingRecord.Date);
			sql.AddParameter("@Amount", SqlDbType.Int, purchasingRecord.Amount);
			sql.AddParameter("@Price", SqlDbType.Decimal, purchasingRecord.Price);
			sql.AddParameter("@Unit", SqlDbType.NVarChar, purchasingRecord.Unit);
			sql.AddParameter("@PurchaserName", SqlDbType.NVarChar, purchasingRecord.PurchaserName);
			sql.AddParameter("@TotalValue", SqlDbType.Decimal, purchasingRecord.TotalValue);
			sql.AddParameter("@PurchaseCate", SqlDbType.NVarChar, purchasingRecord.PurchaseCate);
			sql.AddParameter("@Id", SqlDbType.Int, purchasingRecord.Id);
			sql.ExecuteSql(SqlUpdatePurchasingRecord);
		}

		public void Delete(Core.Business.PurchasingRecord purchasingRecord)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, purchasingRecord.Id);
			sql.ExecuteSql(SqlDeletePurchasingRecord);
		}

		public IList<Core.Business.PurchasingRecord> GetAllPurchasingRecord()
		{
			IList<Core.Business.PurchasingRecord> purchasingRecordlist = new List<Core.Business.PurchasingRecord>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllPurchasingRecord);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.PurchasingRecord purchasingRecord = new Core.Business.PurchasingRecord();

					if (!reader.IsDBNull(0)) purchasingRecord.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) purchasingRecord.PurchasingItem = reader.GetString(1);
					if (!reader.IsDBNull(2)) purchasingRecord.State = reader.GetByte(2);
					if (!reader.IsDBNull(3)) purchasingRecord.Date = reader.GetDateTime(3);
					if (!reader.IsDBNull(4)) purchasingRecord.Amount = reader.GetInt32(4);
					if (!reader.IsDBNull(5)) purchasingRecord.Price = reader.GetDecimal(5);
					if (!reader.IsDBNull(6)) purchasingRecord.Unit = reader.GetString(6);
					if (!reader.IsDBNull(7)) purchasingRecord.PurchaserName = reader.GetString(7);
					if (!reader.IsDBNull(8)) purchasingRecord.TotalValue = reader.GetDecimal(8);
					if (!reader.IsDBNull(9)) purchasingRecord.PurchaseCate = reader.GetString(9);

					purchasingRecord.MarkOld();
					purchasingRecordlist.Add(purchasingRecord);
				}
				reader.Close();
			}
			return purchasingRecordlist;
		}
		#endregion
	}
}
